package org.catdroid.bomba.classes.adapters;

import java.util.List;

import org.catdroid.bomba.classes.Player;
import org.catdroid.bomba.client.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Adapter players
 * @author alorma
 *
 */

public class PlayerListAdapter extends ArrayAdapter<Player> {

	private List<Player> players;
	private Context ctx;
	
	public PlayerListAdapter(Context context, int textViewResourceId,
			List<Player> objects) {
		super(context, textViewResourceId, objects);
		this.players = objects;
		this.ctx = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		
		if ((convertView == null) || !(convertView.getTag() instanceof ViewHolder))
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			convertView = inflater.inflate(R.layout.rowplayer, null);
			
			holder = new ViewHolder();
			
			holder.txt = (TextView) convertView.findViewById(R.id.textRow);
			holder.img = (ImageView) convertView.findViewById(R.id.imageRow);
			holder.layout = (LinearLayout) convertView.findViewById(R.id.layoutRow);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		Player player = this.players.get(position);
		
		holder.txt.setText(player.getName());
		if (player.isReady())
		{
			holder.img.setVisibility(View.VISIBLE);
		}
		else
		{
			holder.img.setVisibility(View.INVISIBLE);
		}
		
		return convertView;
	}
	
}

class ViewHolder {
	TextView txt;
	ImageView img;
	LinearLayout layout;
}
